import 'dart:convert';

import 'package:flutter_soundmagus/model/define.dart';
import 'package:collection/collection.dart';
import 'package:json_annotation/json_annotation.dart';

import 'msg.dart';

part 'SUMMINGModel.g.dart';

enum SUMMINGModelType { ch2, ch4, custom }

enum OutputMode {
  car('car'),
  moto('moto'),
  utv('utv'),
  remote('');

  final String img;
  const OutputMode(this.img);
}

@JsonSerializable(explicitToJson: true, includeIfNull: true)
class SUMMINGModel with CmdStringable {
  SUMMINGModel();

  SUMMINGModelType _type = SUMMINGModelType.custom;
  SUMMINGModelType get type => _type;

  SUMMINGHighModel highModel = SUMMINGHighModel();
  SUMMINGHighModel lowModel = SUMMINGHighModel();

  set type(SUMMINGModelType v) {
    if (_type == v) return;
    _type = v;

    highModel.reset();
    /*
     1.      更新UI，增加了三个按钮，用于合频
     点击：2CH=CH1-1;CH2-2;CH3-1;CH4-2;CH5-12;CH6-12
     4CH=CH1-1;CH2-2;CH3-3;CH4-4;CH5-1234;CH6-1234
     */
    if (_type == SUMMINGModelType.ch2) {
      var chs = [
        [1, null, null, null, null, null, null, null],
        [null, 2, null, null, null, null, null, null],
        [1, null, null, null, null, null, null, null],
        [null, 2, null, null, null, null, null, null],
        [1, null, null, null, null, null, null, null],
        [null, 2, null, null, null, null, null, null],
        [1, null, null, null, null, null, null, null],
        [null, 2, null, null, null, null, null, null],
      ];
      for (var i = 0; i < kChCount; i++) {
        highModel.chs[i] = chs[i];
      }
      return;
    }
    if (_type == SUMMINGModelType.ch4) {
      var chs = [
        [1, null, null, null, null, null, null, null],
        [null, 2, null, null, null, null, null, null],
        [1, null, null, null, null, null, null, null],
        [null, 2, null, null, null, null, null, null],
        [null, null, 3, null, null, null, null, null],
        [null, null, null, 4, null, null, null, null],
        [null, null, null, null, 5, null, null, null],
        [null, null, null, null, null, 6, null, null],
      ];
      for (var i = 0; i < kChCount; i++) {
        highModel.chs[i] = chs[i];
      }
      return;
    }
  }

  bool isHightHave({required int ch, required int selectedCH}) {
    var obj =
        highModel.chs[selectedCH].firstWhereOrNull((element) => element == ch);
    return obj != null;
  }

  factory SUMMINGModel.fromJson(Map<String, dynamic> json) =>
      _$SUMMINGModelFromJson(json);

  Map<String, dynamic> toJson() => _$SUMMINGModelToJson(this);

  @override
  String toCmdStr() {
    var str = '';
    for (int selectedCH = 0; selectedCH < kChCount; selectedCH++) {
      ZFSummingMsg msg = ZFSummingMsg();
      msg.ch.value = selectedCH + 0xc1;

      int sum = 0;
      for (int i = 1; i < kChCountIn; i++) {
        var state = isHightHave(ch: i, selectedCH: selectedCH) ? 0x01 : 0x00;
        var kp = msg.properties[i + 3] as ZPBDataType;
        kp.value = state;
        if (state > 0) {
          sum++;
        }
      }
      // msg.sum.value = sum;
      str += '${msg.toHexString()}\n';
    }
    return str;
  }
}

@JsonSerializable(explicitToJson: true, includeIfNull: true)
class SUMMINGHighModel {
  List<List<int?>> chs = [];

  SUMMINGHighModel() {
    _init();
  }

  _init() {
    chs = List.generate(
        kChCount, (i) => List.generate(kChCount, (index) => null));
  }

  void reset() {
    _init();
  }

  factory SUMMINGHighModel.fromJson(Map<String, dynamic> json) =>
      _$SUMMINGHighModelFromJson(json);

  Map<String, dynamic> toJson() => _$SUMMINGHighModelToJson(this);

  @override
  String toString() {
    return 'SUMMINGHighModel=>' + json.encode(chs);
  }
}
